from flask import (
    views,
    Blueprint,
    request,
)
from network.NetworkModel_pb2 import (
    PBEmpty,
    RegisterRequest,
    RegisterValidateRequest,
    LoginRequest,
    UserNameChangeRequest,
    UserAvatarChangeRequest
)
from network import (
    success_response,
    failure_response,
    req_data_error_response,
    req_data_miss_parameter,
    parse_data,
)
from .models import User
from extens import db
from utils import ImageTool
from time import time

bp = Blueprint("user", __name__, url_prefix="/user")


class RegisterValidateView(views.MethodView):
    def post(self):
        req = parse_data(RegisterValidateRequest(), request)
        if req is None:
            return req_data_error_response
        user = User.query.filter_by(account=req.account).first()
        if user is None:
            return success_response(PBEmpty())
        else:
            return failure_response('账号已存在')


class RegisterView(views.MethodView):
    def post(self):
        req = parse_data(RegisterRequest(), request)
        if req is None:
            return req_data_error_response
        user = User.query.filter_by(account=req.account).first()
        if user is not None:
            return failure_response('账号已存在')
        user = User(account=req.account, password=req.password, name=req.name)
        db.session.add(user)
        db.session.commit()
        return success_response(user.get_user_info())


class LoginView(views.MethodView):
    def post(self):
        req = parse_data(LoginRequest(), request)
        if req is None:
            return req_data_error_response
        user = User.query.filter_by(account=req.account).first()
        if user is None or user.check_password(req.password) is False:
            return failure_response('用户名或密码错误')
        return success_response(user.get_user_info())


class ChangeUserNameView(views.MethodView):
    def post(self):
        req = parse_data(UserNameChangeRequest(), request)
        if req is None:
            return req_data_error_response
        user = User.query.filter_by(id=req.uid).first()
        if user is None:
            return failure_response('用户id不存在')
        user.name = req.name
        db.session.add(user)
        db.session.commit()
        return success_response(user.get_user_info())


class ChangeUserAvatarView(views.MethodView):
    def post(self):
        req = parse_data(UserAvatarChangeRequest(), request)
        if req is None:
            return req_data_error_response
        user = User.query.filter_by(id=req.uid).first()
        if user is None:
            return failure_response('用户id不存在')
        image_data = req.imageData
        if image_data is None:
            return req_data_miss_parameter('缺少图片数据')
        filename = 'avatars{}{}'.format(user.id, int(time()))
        tool = ImageTool(300, 300, 'avatars', filename)
        avatar_url = tool.process_image(image_data)
        user.avatarUrl = avatar_url
        db.session.add(user)
        db.session.commit()
        return success_response(user.get_user_info())


bp.add_url_rule('/registerValidate/', view_func=RegisterValidateView.as_view('register_validate_view'))
bp.add_url_rule('/register/', view_func=RegisterView.as_view('register_view'))
bp.add_url_rule('/login/', view_func=LoginView.as_view('login_view'))
bp.add_url_rule('/changeUserName/', view_func=ChangeUserNameView.as_view('change_user_name_view'))
bp.add_url_rule('/changeUserAvatar/', view_func=ChangeUserAvatarView.as_view('change_user_avatar_view'))
